public class MyList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void createList(){
        ListNode node1 = new ListNode(16);
        ListNode node2 = new ListNode(18);
        ListNode node3 = new ListNode(26);
        ListNode node4 = new ListNode(84);
        ListNode node5 = new ListNode(108);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    public int size(){
        ListNode multiple = head;
        int count = 0;
        while (multiple != null){
            multiple = multiple.next;
            count++;
        }
        return count;
    }

    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display(ListNode newHead){ //重载打印方法，打印新接收的链表，不改变原有的链表。
        ListNode cur = newHead;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public ListNode middleDisplay(){  //打印链表中间位置的值，若是偶数则打印第二个。
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    public ListNode FindKtoTail(int k){ //输入一个值并输出倒数第k个节点的值
        if (k <= 0 || k > size()){
            System.out.println("输入的数字不合法");
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode mergeTwoLists(ListNode headA,ListNode headB){
        ListNode newHead = new ListNode(-1);
        ListNode temHead = newHead;

        while (headA != null || headB != null){
            if (headA.val > headB.val){
                temHead.next = headB;
                headB = headB.next;
            }else {
                temHead.next = headA;
                headA = headB.next;
            }
            temHead = temHead.next;
        }
        if (headA != null){
            temHead.next = headA;
        }
        if (headB != null){
            temHead.next = headB;
        }
        return newHead.next;
    }
    public ListNode reverseList(){ //将链表进行翻转
        if (head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public void allRemove(int key){ //删除所有val为key的节点
        if (head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;

        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }
    public void clear(){ //清除所有节点
        this.head = null;
    }
}
